//
//  MYHomeTableViewCell.swift
//  swift2.0项目开始
//
//  Created by 龚昕炜 on 16/8/1.
//  Copyright © 2016年 龚昕炜. All rights reserved.
//

import UIKit

class MYHomeTableViewCell: UITableViewCell {

    /** 约束(底部) */
    var constraint: Constraint?
    
    var cellViewModel: MYHomeCellViewModel? {
        
        didSet {
            /** 传递数据给View */
            originalView.originalViewModel = cellViewModel
            retweetView.retweetViewModel = cellViewModel
            footerView.footerViewModel = cellViewModel
            /** 清除约束 */
            /** 
               关于约束：
               1.只有当原视图，相对位置，相对视图三者都一样的时候才是相同约束;
               2.snapKit的updateConstraints对待约束：当是相同约束的时候会更新约束
            当是不相同约束的时候会认为是新约束添加上去；
               3.当约束冲突的时候会在冲突的约束中任选一个展示出来；
            
              */
            constraint?.uninstall()
            
            /** 判断有没有转发微博 */
            if ((cellViewModel?.statusModel?.retweeted_status) != nil) {
                /** 有转发微博 */
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                /** 展示 */
                retweetView.hidden = false
                
//                print(cellViewModel?.statusModel?.retweeted_status!.pic_urls)
            } else {
                /** 没有转发微博 */
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                /** 隐藏 */
                retweetView.hidden = true
            
            }
            
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /** 添加子控件设置约束 */
    private func setUpUI() {
        
        contentView.addSubview(originalView)
        contentView.addSubview(footerView)
        contentView.addSubview(retweetView)
        
        /** 原创 */
        originalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
//          make.bottom.equalTo(retweetView.snp_top).constraint
        }
        
        /** 转发 */
        retweetView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(originalView.snp_bottom)
            make.right.equalTo(contentView.snp_right)
//            make.bottom.equalTo(footerView.snp_top)
        }
        
        /** 底部tool */
        footerView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.bottom.equalTo(contentView.snp_bottom)
            /** 给约束属性赋值 */
            self.constraint = make.top.equalTo(retweetView.snp_bottom).constraint
        }
        
        /** contentView(不设置自动计算行高可能不准确) */
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(self.snp_bottom)
        }
    }
    
    /** 懒加载 */
    /** 源 */
    private lazy var originalView: MYOrginalView = MYOrginalView()
    
    /** 转 */
    private lazy var retweetView: MYRetweetView = MYRetweetView()
    
    /** 底部tool */
    private lazy var footerView: MYFootView = MYFootView()

}
